﻿<%@ WebHandler Language="C#" Class="SocLinke" %>

using System;
using System.Web;
using Json.net;
using DBUtility;
using System.Data;

public class SocLinke : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        if (PublicFunction.GetRequstQueryString(context.Request, "act") == "BigSort")
        {
            context.Response.Write(GetBigSortJson());
        }
        if (PublicFunction.GetRequstQueryString(context.Request, "act") == "SecSort")
        {
            context.Response.Write(GetSecSortJson(PublicFunction.GetRequstQueryString(context.Request, "SortID")));
        }
        if (PublicFunction.GetRequstQueryString(context.Request, "act") == "PropName")
        {
            context.Response.Write(GetPropNameJson());
        }
        if (PublicFunction.GetRequstQueryString(context.Request, "act") == "PropUnit")
        {
            context.Response.Write(GetPropUnitJson());
        }
        if (PublicFunction.GetRequstQueryString(context.Request, "act") == "dep")
        {
            if (PublicFunction.GetRequstQueryString(context.Request, "dep")=="")
            context.Response.Write(GetDepJson(""));
            else
                context.Response.Write(GetDepJson(PublicFunction.GetRequstQueryString(context.Request, "dep")));
        }  
    }

    String GetBigSortJson()
    {
        JsonFun jf=new JsonFun();
        return jf.GetComboxJsonStr(GetDataTableBySql("SELECT SORTID,SORTNAME  FROM SocLinkeSort where RIGHT(SORTID,2)='00'"), "SORTID", "SORTNAME");
    }

    String GetPropNameJson()
    {
        JsonFun jf = new JsonFun();
        return jf.GetComboxJsonStr(GetDataTableBySql("SELECT distinct SocPropname  FROM SocProp"), "SocPropname", "SocPropname");
    }

    String GetPropUnitJson()
    {
        JsonFun jf = new JsonFun();
        return jf.GetComboxJsonStr(GetDataTableBySql("SELECT distinct SocPropUnit  FROM [SocProp]"), "SocPropUnit", "SocPropUnit");
    }
    
    String GetDepJson(String depid)
    {
        JsonFun jf = new JsonFun();
        if(depid=="" || depid=="1300000")
             return jf.GetComboxJsonStr(GetDataTableBySql("SELECT DepartmentID,shortname FROM [FireMis].[dbo].[Account_Departments] where (right(DepartmentID,3)='000' or SUBSTRING(DepartmentID,5,1)='2' ) "), "DepartmentID", "shortname");
        else if(depid.Substring(4,3)=="000")
            return jf.GetComboxJsonStr(GetDataTableBySql("SELECT DepartmentID,shortname FROM [FireMis].[dbo].[Account_Departments] where (right(DepartmentID,3)='000' or SUBSTRING(DepartmentID,5,1)='2' ) and left(DepartmentID,4)=left('" + depid + "',4)"), "DepartmentID", "shortname");
        else
            return jf.GetComboxJsonStr(GetDataTableBySql("SELECT DepartmentID,shortname FROM [FireMis].[dbo].[Account_Departments] where (right(DepartmentID,3)='000' or SUBSTRING(DepartmentID,5,1)='2' ) and DepartmentID='" + depid + "'"), "DepartmentID", "shortname");
    }

    String GetSecSortJson(String bigsort)
    {
        JsonFun jf = new JsonFun();
        return jf.GetComboxJsonStr(GetDataTableBySql("SELECT SORTID,SORTNAME  FROM SocLinkeSort where left(SORTID,2)+'00'='" + bigsort + "' and RIGHT(SORTID,2)<>'00'"), "SORTID", "SORTNAME");
    }

    DataTable GetDataTableBySql(String sql)
    {
        SQLHelper db = new SQLHelper();
        return db.GetDataTableBySqlStr(sql);
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}